package com.itqian.controller.seller;

import com.itqian.common.exception.Assert;
import com.itqian.common.exception.EorderException;
import com.itqian.common.exception.ResponseEnum;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import com.itqian.seller.dto.AmountLineDTO;
import com.itqian.seller.dto.BarDTO;
import com.itqian.seller.dto.OrderLineDTO;
import com.itqian.seller.mapper.FoodInfoMapper;
import com.itqian.seller.mapper.OrderMasterMapper;
import com.itqian.seller.service.OrderMasterService;
import com.itqian.seller.vo.OrderVO;
import com.itqian.system.entity.Constants;
import com.itqian.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author shishuqian
 * date 2022/3/23
 * time 10:32
 **/
@RestController
@RequestMapping("/seller/order")
public class SellerOrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private FoodInfoMapper foodInfoMapper;



    /**
     *
     * @author shishuqian
     * @desc  分页查询此店铺的所有订单信息（用户已完成订单支付的）
     */
    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@RequestHeader("token") String token, @PathVariable Integer page, @PathVariable Integer size){
        //参数非空校验
        Assert.notNull(page, ResponseEnum.PAGE_NOTNULL);
        Assert.notNull(size,ResponseEnum.SIZE_NOTNULL);

        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        Map<String, Object> list = orderMasterService.list(storeId, page, size);
        return ResultVOUtil.success(list);
    }

    /**
     *
     * @author shishuqian
     * @desc  查询订单的详细信息
     */
    //@GetMapping("/detail/{orderId}")
    //public ResultVO detail(@PathVariable String orderId){
    //    //参数非空校验
    //    Assert.notBlank(orderId,ResponseEnum.PARAM_NOTNULL);
    //
    //    OrderVO detail = orderMasterService.detail(orderId);
    //    return ResultVOUtil.success(detail);
    //}
    //int 类型的order_id
    @GetMapping("/detail/{orderId}")
    public ResultVO detail(@PathVariable Integer orderId){
        //参数非空校验
        Assert.notNull(orderId,ResponseEnum.PARAM_NOTNULL);

        OrderVO detail = orderMasterService.detail(orderId);
        return ResultVOUtil.success(detail);
    }

    /**
     *
     * @author shishuqian
     * @desc   根据关键字和关键字类型进行模糊查询
     *  根据订单号，买家名，手机号等
     */
    @GetMapping("/like/{type}/{keyword}/{page}/{size}")
    public ResultVO like(@RequestHeader("token") String token,
                         @PathVariable String type,
                         @PathVariable String keyword,
                         @PathVariable Integer page,
                         @PathVariable Integer size){
        //校验是否支持此类型的查询
        Assert.notBlank(type,ResponseEnum.TYPE_NOTNULL);
        String[] as = new String[]{"order_id","buyer_name","buyer_phone"};
        List<String> list = Arrays.asList(as);
        if (!list.contains(type)){
            throw new EorderException("不支持此类型的查询...");
        }
        //参数非空校验,合法性校验
        Assert.notBlank(keyword,ResponseEnum.KEYWORD_NOTNULL);
        Assert.notNull(page, ResponseEnum.PAGE_NOTNULL);
        Assert.notNull(size,ResponseEnum.SIZE_NOTNULL);

        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        Map<String, Object> map = orderMasterService.like(storeId, page, size, type, keyword);
        return ResultVOUtil.success(map);
    }

    /**
     *
     * @author shishuqian
     * @desc   商家根据订单id删除订单(并不是真正删除，而是将seller_deleted置为true)
     */

    //改为int 类型order_id
    @DeleteMapping("/delete/{orderId}")
    public ResultVO delete(@PathVariable Integer orderId){
        //参数非空校验
        Assert.notNull(orderId,ResponseEnum.PARAM_NOTNULL);
        boolean b = orderMasterService.delete(orderId);
        if (!b){
            return ResultVOUtil.fail("删除订单失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    /**
     *
     *  统计该店铺几天内的销售额折线图，最多50天
     */
    @GetMapping("/amountLine/{days}")
    public ResultVO amountLine(@RequestHeader("token") String token,@PathVariable Integer days){
        //参数校验
        Assert.notNull(days,ResponseEnum.PARAM_NOTNULL);
        if (days<1 || days> Constants.STATISTICS_NUM){
            throw new EorderException("参数异常，统计天数超过规定...");
        }

        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        List<AmountLineDTO> amountLine = orderMasterMapper.getAmountLine(days, storeId);
        return ResultVOUtil.success(amountLine);
    }

    /**
     *
     *  统计该店铺几天内的销售量折线图，最多50天
     */
    @GetMapping("/orderLine/{days}")
    public ResultVO orderLine(@RequestHeader("token") String token,@PathVariable Integer days){
        //参数校验
        Assert.notNull(days,ResponseEnum.PARAM_NOTNULL);
        if (days<1 || days>Constants.STATISTICS_NUM){
            throw new EorderException("参数异常，统计天数超过规定...");
        }

        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        List<OrderLineDTO> orderLine = orderMasterMapper.getOrderLine(days, storeId);
        return ResultVOUtil.success(orderLine);
    }


    /**
     *  统计该店铺所有菜品销量的柱状图
     */
    @GetMapping("/bar")
    public ResultVO bar(@RequestHeader("token") String token){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        List<BarDTO> bar = foodInfoMapper.getBar(storeId);

        return ResultVOUtil.success(bar);
    }

    /**
     *  统计该店铺所有菜品分类销量的柱状图
     */
    @GetMapping("/categoryBar")
    public ResultVO categoryBar(@RequestHeader("token") String token){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        List<BarDTO> list = orderMasterService.categoryBar(storeId);

        return ResultVOUtil.success(list);
    }


}
